package com.jxf.demo.service.impl;

import com.jxf.demo.dto.DXFArcInfo;
import com.jxf.demo.dto.DXFBoundingRectangle;
import com.jxf.demo.dto.DXFPointInfo;
import com.jxf.demo.service.AbstractDXFParseService;
import org.kabeja.dxf.DXFArc;
import org.kabeja.dxf.helpers.Point;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Service
public class DXFArcParseService extends AbstractDXFParseService {
    @Override
    public List<DXFPointInfo> doParse(List lineEntities) {
        List<DXFArcInfo> dxfArcInfos = parseArc(lineEntities);
        return new ArrayList<>();
    }

    @Override
    public String getType() {
        return "ARC";
    }

    public List<DXFArcInfo> parseArc(List arcEntities) {
        List<DXFArcInfo> list = new ArrayList<>();
        for (Object arcEntity : arcEntities) {
            DXFArc arc = (DXFArc) arcEntity;
            // 圆形坐标
            Point centerPoint = arc.getCenterPoint();
            DXFArcInfo arcInfo = DXFArcInfo.builder()
                    .centerPoint(centerPoint)
                    .radius(arc.getRadius())
                    .startAngle(arc.getStartAngle())
                    .endAngle(arc.getEndAngle()).build();
            // 直径
            BigDecimal diameter = BigDecimal.valueOf(arcInfo.getRadius()).multiply(new BigDecimal("2"));
            // 外接矩形
            DXFBoundingRectangle boundingRectangle = DXFBoundingRectangle.builder()
                    .width(diameter.doubleValue())
                    .height(arcInfo.getRadius())
                    .x(centerPoint.getX() - arc.getRadius())
                    .y(centerPoint.getY()).build();
            arcInfo.setBoundingRectangle(boundingRectangle);
            list.add(arcInfo);
        }
        return list;
    }
}
